import connect from "@/utils/mongodb";
import User from "@/model/User";
import { NextResponse } from "next/server";

export const POST = async (request) => {
    try {
        const { email } = await request.json();
        if (!email) {
            return NextResponse.json(
                { message: "请输入邮箱地址", isExisted: false },
                { status: 400 }
            );
        }

        await connect();
        // 检查数据库中是否存在该邮箱
        const existingUser = await User.findOne({ email });

        return NextResponse.json({
            isExisted: !!existingUser, // 如果存在则返回true，否则false
            message: existingUser ? "邮箱已被注册" : "邮箱可用"
        });
    } catch (error) {
        console.error("检查邮箱失败:", error);
        return NextResponse.json(
            { message: "检查邮箱失败", isExisted: false },
            { status: 500 }
        );
    }
};